home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #include "PrintPS.h"
- #include "VCal.h"
- #include "Entry.h"
- #include "Info.h"
- #include "Utils.h"
- #include "Preferences.h"
- #include <Vk/VkHelpAPI.h>
- #include <Vk/VkWarningDialog.h>
-
- #include <Sgm/PrintBox.h>
-
- static int count;
- static Arg args[10];
-
- //
- // PostScript from pcal
- //
-
- /*
- * pheader - provides the PostScript routines
- */
- static const char *pheader[] = {
- "%!",
- "/titlefont /Times-Bold def",
- "/dayfont /Helvetica-Bold def",
- "/month_names [ (January) (February) (March) (April) (May) (June) (July)",
- "\t\t(August) (September) (October) (November) (December) ] def",
- "/prtnum { 3 string cvs show} def",
- "/drawgrid {\t\t% draw calendar boxes",
- "\tdayfont findfont 10 scalefont setfont",
- "\t0 1 6 {",
- "\t\tdup dup 100 mul 40 moveto",
- "\t\t[ (Sunday) (Monday) (Tuesday) (Wednesday) (Thursday) (Friday) (Saturday) ] exch get",
- "\t\t100 center",
- "\t\t100 mul 35 moveto",
- "\t\t1.0 setlinewidth",
- "\t\t0 1 5 {",
- "\t\t\tgsave",
- "\t\t\t100 0 rlineto ",
- "\t\t\t0 -80 rlineto",
- "\t\t\t-100 0 rlineto",
- "\t\t\tclosepath stroke",
- "\t\t\tgrestore",
- "\t\t\t0 -80 rmoveto",
- "\t\t} for",
- "\t} for",
- "} def",
- "/drawnums {\t\t% place day numbers on calendar",
- "\tdayfont findfont 30 scalefont setfont",
- "\t/start startday def",
- "\t/days ndays def",
- "\tstart 100 mul 5 add 10 rmoveto",
- "\t1 1 days {",
- "\t\t/day exch def",
- "\t\tgsave",
- #ifndef SATBLK
- "\t\tday start add 7 mod 0 eq",
- "\t\t{",
- "\t\t\tsubmonth 0 eq",
- "\t\t\t{",
- "\t\t\t\t.8 setgray",
- "\t\t\t} if",
- "\t\t} if",
- #endif
- "\t\tday start add 7 mod 1 eq",
- "\t\t{",
- "\t\t\tsubmonth 0 eq",
- "\t\t\t{",
- "\t\t\t\t.8 setgray",
- "\t\t\t} if",
- "\t\t} if",
- "\t\tday prtnum",
- "\t\tgrestore",
- "\t\tday start add 7 mod 0 eq",
- "\t\t{",
- "\t\t\tcurrentpoint exch pop 80 sub 5 exch moveto",
- "\t\t}",
- "\t\t{",
- "\t\t\t100 0 rmoveto",
- "\t\t} ifelse",
- "\t} for",
- "} def",
- "/drawfill {\t\t% place fill squares on calendar",
- "\t/start startday def",
- "\t/days ndays def",
- "\t0 35 rmoveto",
- "\t1.0 setlinewidth",
- "\t0 1 start 1 sub {",
- "\t\tgsave",
- "\t\t.9 setgray",
- "\t\t100 0 rlineto ",
- "\t\t0 -80 rlineto",
- "\t\t-100 0 rlineto",
- "\t\tclosepath fill",
- "\t\tgrestore",
- "\t\t100 0 rmoveto",
- "\t} for",
- "\tsubmonth 1 eq",
- "\t{",
- "\t\t/lastday 42 def",
- "\t\t600 -365 moveto",
- "\t}",
- "\t{",
- "\t\t/lastday 40 def",
- "\t\t400 -365 moveto",
- "\t} ifelse",
- "\tlastday -1 ndays start 1 add add",
- "\t{",
- "\t\t/day exch def",
- "\t\tgsave",
- "\t\t.9 setgray",
- "\t\t100 0 rlineto ",
- "\t\t0 -80 rlineto",
- "\t\t-100 0 rlineto",
- "\t\tclosepath fill",
- "\t\tgrestore",
- "\t\tday 7 mod 1 eq",
- "\t\t{",
- "\t\t\t600 -365 80 add moveto",
- "\t\t}",
- "\t\t{",
- "\t\t\t-100 0 rmoveto",
- "\t\t} ifelse",
- "\t} for",
- "} def",
- "/isleap {\t\t% is this a leap year?",
- "\tyear 4 mod 0 eq\t\t% multiple of 4",
- "\tyear 100 mod 0 ne \t% not century",
- "\tyear 1000 mod 0 eq or and\t% unless it's a millenia",
- "} def",
- "/days_month [ 31 28 31 30 31 30 31 31 30 31 30 31 ] def",
- "/ndays {\t\t% number of days in this month",
- "\tdays_month month 1 sub get",
- "\tmonth 2 eq\t% Feb",
- "\tisleap and",
- "\t{",
- "\t\t1 add",
- "\t} if",
- "} def",
- "/startday {\t\t% starting day-of-week for this month",
- "\t/off year 2000 sub def\t% offset from start of epoch",
- "\toff",
- "\toff 4 idiv add\t\t% number of leap years",
- "\toff 100 idiv sub\t% number of centuries",
- "\toff 1000 idiv add\t% number of millenia",
- "\t6 add 7 mod 7 add \t% offset from Jan 1 2000",
- "\t/off exch def",
- "\t1 1 month 1 sub {",
- "\t\t/idx exch def",
- "\t\tdays_month idx 1 sub get",
- "\t\tidx 2 eq",
- "\t\tisleap and",
- "\t\t{",
- "\t\t\t1 add",
- "\t\t} if",
- "\t\t/off exch off add def",
- "\t} for",
- "\toff 7 mod\t\t% 0--Sunday, 1--monday, etc.",
- "} def",
- "/center {\t\t% center string in given width",
- "\t/width exch def",
- "\t/str exch def width str ",
- "\tstringwidth pop sub 2 div 0 rmoveto str show",
- "} def",
- "/calendar",
- "{",
- "\ttitlefont findfont 48 scalefont setfont",
- "\t0 60 moveto",
- "\t/month_name month_names month 1 sub get def",
- "\tmonth_name show",
- "\t/yearstring year 10 string cvs def",
- "\t700 yearstring stringwidth pop sub 60 moveto",
- "\tyearstring show",
- "\t0 0 moveto",
- "\tdrawnums",
- "\t0 0 moveto",
- "\tdrawfill",
- "\t0 0 moveto",
- "\tdrawgrid",
- "} def",
- "/daytext {",
- "\t/Helvetica-Narrow findfont 6 scalefont setfont",
- "\t/mytext\texch def /myday exch def",
- "\tstartday myday 1 sub add dup 7 mod 100 mul 5 add % gives column",
- "\texch 7 idiv -80 mul % gives row",
- "\tdup /ypos exch def moveto",
- "\t/LM currentpoint pop def /RM LM 95 add def",
- " mytext { dup (.p) eq { crlf pop} {prstr ( ) show} ifelse } forall",
- "} def",
- "/crlf {",
- " ypos 8 sub /ypos exch def LM ypos moveto",
- "} def",
- "/prstr {",
- " dup stringwidth pop currentpoint pop",
- " add RM gt {crlf 15 0 rmoveto} if show",
- "} def",
- "/printmonth {",
- "\t90 rotate",
- "\t50 -120 translate",
- "\t/submonth 0 def",
- "\tcalendar",
- "\tmonth 1 sub 0 eq",
- "\t{",
- "\t\t/lmonth 12 def",
- "\t\t/lyear year 1 sub def",
- "\t}",
- "\t{",
- "\t\t/lmonth month 1 sub def",
- "\t\t/lyear year def",
- "\t} ifelse",
- "\tmonth 1 add 13 eq",
- "\t{",
- "\t\t/nmonth 1 def",
- "\t\t/nyear year 1 add def",
- "\t} ",
- "\t{",
- "\t\t/nmonth month 1 add def",
- "\t\t/nyear year def",
- "\t} ifelse",
- "\t/savemonth month def",
- "\t/saveyear year def",
- "\t/submonth 1 def",
- "\t/year lyear def",
- "\t/month lmonth def",
- "\tgsave",
- "\t500 -365 translate",
- "\tgsave",
- "\t.138 .138 scale",
- "\t10 -120 translate",
- "\tcalendar",
- "\tgrestore",
- "\t/submonth 1 def",
- "\t/year nyear def",
- "\t/month nmonth def",
- "\t100 0 translate",
- "\tgsave",
- "\t.138 .138 scale",
- "\t10 -120 translate",
- "\tcalendar",
- "\tgrestore",
- "\t/month savemonth def",
- "\t/year saveyear def",
- "\t/submonth 0 def",
- "\tgrestore",
- "} def",
- (char *)0,
- };
-
- PrintPS::PrintPS(VCal *o)
- {
- owner = o;
-
- printBox = NULL;
- printFile = NULL;
- }
-
- PrintPS::~PrintPS()
- {
- }
-
- /**********************************************************************/
-
- void
- PrintPS::printMonthUI(int month, int year)
- {
- char tempFile[256], str[MAXSTR];
- FILE *fd;
-
- if (printFile) {
- theWarningDialog->post("Sorry, but a print dialog is still active");
- return;
- }
- if (getenv("TMPDIR")) {
- sprintf(tempFile, "%s/vcalPrint-XXXXXX", getenv("TMPDIR"));
- } else {
- sprintf(tempFile, "/tmp/vcalPrint-XXXXXX");
- }
- mktemp(tempFile);
- if (fd = fopen(tempFile, "w")) {
- printMonth(fd, month, year);
- fclose(fd);
- displayPrintUI(tempFile);
- } else {
- sprintf(str, "Couldn't create temporary file %s for printing", tempFile);
- theWarningDialog->post(str);
- }
- }
-
- void
- PrintPS::printMonth(FILE *fd, int month, int year)
- {
- int days, day, each;
- MemoryInfo *dayInfo;
- Entry *entry;
- RepeatingEntry *rentry;
- char str[MAXSTR];
-
- printHeader(fd);
- fprintf(fd, "/year %d def\n", year);
- fprintf(fd, "/month %d def\n", month);
- fprintf(fd, "printmonth\n");
-
- days = NumberOfDays(month, year);
- for (day=1; day<=days; day++) {
- dayInfo = owner->findDateInfo(day, month, year);
- owner->initCollect();
- if (dayInfo) {
- dayInfo->rewind();
- while (entry = dayInfo->nextEntry()) {
- owner->collectSingleEntry(entry);
- }
- }
- rentry = owner->getRepeatingEntries();
- while (rentry->next()) {
- rentry = rentry->next();
- if (rentry->repeatApplies(day, month, year)) {
- owner->collectSingleEntry(rentry);
- }
- }
- if (owner->collectedCount()) {
- owner->sortCollection();
- fprintf(fd, "%d [\n", day);
- for (each=0; each<owner->collectedCount(); each++) {
- entry = owner->collectedEntry(each);
- formatTime(entry->start() / 60, entry->start() % 60,
- thePreferences->clock24(),
- str);
- strcat(str, "-");
- formatTime((entry->start()+entry->length()) / 60,
- (entry->start()+entry->length()) % 60,
- thePreferences->clock24(),
- str+strlen(str));
- strcat(str, " ");
- strcat(str, entry->text());
- emitWords(fd, str);
- if (each < owner->collectedCount()-1) {
- fprintf(fd, "(.p)\n");
- }
- }
- fprintf(fd, "] daytext\n");
- }
- }
-
- fprintf(fd, "showpage\n");
- }
-
- /**********************************************************************/
-
- void
- PrintPS::printHeader(FILE *fd)
- {
- const char **ap;
-
- for (ap=pheader; *ap; ap++) {
- fputs(*ap, fd);
- fputs("\n", fd);
- }
- }
-
- void
- PrintPS::emitWords(FILE *fd, char *str)
- {
- char *dup, *p;
-
- dup = strdup(str);
- if (p = strtok(dup, " \t")) {
- fprintf(fd, "(%s)\n", p);
- while (p = strtok(NULL, " \t\n")) {
- fprintf(fd, "(%s)\n", p);
- }
- }
- free(dup);
- }
-
- void
- PrintPS::displayPrintUI(char *file)
- {
- printFile = strdup(file);
- count = 0;
- XtSetArg(args[count], PuiNfilename, printFile); count++;
- if (printBox) {
- XtSetValues(printBox, args, count);
- } else {
- printBox = PuiCreatePrintDialog(owner->baseWidget(), "printBox",
- args, count);
- XtAddCallback(printBox, PuiNcancelCallback, cancel_cb, (XtPointer) this);
- XtAddCallback(printBox, PuiNhelpCallback, help_cb, (XtPointer) this);
- XtAddCallback(printBox, PuiNjobInfoCallback, print_cb, (XtPointer) this);
- XtAddCallback(printBox, PuiNerrorCallback, error_cb, (XtPointer) this);
- XtAddCallback(printBox, PuiNoptionErrorCallback, error_cb,
- (XtPointer) this);
- }
- XtManageChild(printBox);
- }
-
- void
- PrintPS::printComplete()
- {
- if (printFile) {
- unlink(printFile);
- free(printFile);
- printFile = NULL;
- /* Reuse the print box
- XtDestroyWidget(printBox);
- printBox = NULL;
- */
- }
- }
-
- /**********************************************************************/
-
- void
- PrintPS::cancel_cb(Widget, XtPointer client_data, XtPointer)
- {
- PrintPS *obj = (PrintPS *) client_data;
-
- obj->printComplete();
- }
-
- void
- PrintPS::help_cb(Widget, XtPointer, XtPointer)
- {
- SGIHelpMsg(NULL, NULL, NULL);
- }
-
- void
- PrintPS::print_cb(Widget, XtPointer client_data, XtPointer call_data)
- {
- PrintPS *obj = (PrintPS *) client_data;
- PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*) call_data;
- char str[256];
-
- sprintf(str, "request id is %s", pb_cb->job_info->job_id);
- theWarningDialog->post(str);
- obj->printComplete();
- }
-
- void
- PrintPS::error_cb(Widget, XtPointer, XtPointer call_data)
- {
- PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*) call_data;
-
- if (pb_cb->reason == PuiCR_OPT_ERROR) {
- theWarningDialog->post(strerror(pb_cb->error_code));
- } else {
- theWarningDialog->post(SLErrorString(pb_cb->error_code));
- }
- }
-
-